﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTabControl;
using olierFrm.WebFormDev;
using olierFrm.DataBase.FrameWork.StdQuery;

namespace WebFormDev.StdQueryPack
{
    public partial class SubQueryPage : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            ManageSystem.IsLogin();
            ManageSystem.SetTheme(Page);
            //没有QueryName只显示菜单，为了重复利用该页面（系统首页和StdQuery）

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var QueryName = Request.QueryString["QueryName"];
            ASPxPageControl pageControl = new ASPxPageControl();
            //var tabC = new ASPxPageControl();
            //container.Controls.Add(tabC);
            StdQuerySQL sqs = new StdQuerySQL(QueryName);

            SubQuery subQuery = new SubQuery(sqs.StdQuery.SubQuery);
            foreach (var t in subQuery.SciptObject)
            {
                StdQuerySQL dbStdQuery = new StdQuerySQL(t.Value.HintQueryName);



                //var subQueryInfo = subQuery.SciptObject[t.Key];
                //var pms = subQueryInfo.ParamMatch.Split(';').ToList();
                //foreach (var p in dbStdQuery.StdQueryParams)
                //{
                //    //寻找匹配参数
                //    var pm = pms.Where(pp => pp.IndexOf(p.ParamName + "=") != -1).ToList();
                //    if (pm.Count() != 0)
                //    {
                //        var ParamValue = Request.QueryString[pm.First().Split('=')[1]];
                //        dbStdQuery.SetParam(p.ParamName, ParamValue);
                //    }
                //    else
                //    {
                //        var ParamValue = Request.QueryString[p.ParamName];
                //        if (ParamValue != null)
                //        {
                //            dbStdQuery.SetParam(p.ParamName, ParamValue);
                //        }
                //    }
                //}
                //dbStdQuery.AddSystemParams();
                //var dtSource = dbStdQuery.ExecuteSQL();

                //StdQuery.Grid uiGrid = new StdQuery.Grid(dtSource, dbStdQuery);
                //var detailGrid = uiGrid.CreateGridView();

                //if (dtSource.Rows.Count != 0)
                //    detailGrid.Settings.ShowVerticalScrollBar = true;

                //detailGrid.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.Control;
                //detailGrid.HideColumn(t.Value.HideFields.Split(';').ToList());
                //UIFrm.BS.StdQuery.Chart chart = new StdQuery.Chart(dbStdQuery);

                //if (subQuery.SciptObject.Count > 1 )
                //{
                //if (pageControl.TabPages.Count == 0)
                //{
                //    pageControl.Width = new Unit("100%");
                //}

                var tp = new DevExpress.Web.ASPxTabControl.TabPage(t.Key);
                tp.Controls.Add(new LiteralControl("<iframe class='iFrameDetailGrid' style='width:100%; height:100%;' frameborder='no' framespacing='0' border='0' src='DetailGridPage.aspx" + HttpContext.Current.Request.Url.Query.Replace("QueryName", "MasterQueryName") + "&QueryName=" + t.Value.HintQueryName + "'></iframe>"));
                //tp.Controls.Add(detailGrid);
                //detailGrid.SettingsDetail.AllowOnlyOneMasterRowExpanded = true;
                pageControl.TabPages.Add(tp);
                if (dbStdQuery.StdQuery.InCharts != null && dbStdQuery.StdQuery.InCharts.Trim().Length != 0)
                {
                    InChart ic = new InChart(dbStdQuery.StdQuery.InCharts);
                    //PageControlChart.AutoPostBack = true;
                    //PageControlChart.ActiveTabChanged += new TabControlEventHandler(tabGridAndChart_ActiveTabChanged);
                    int index = 0;
                    foreach (var i in ic.SciptObject)
                    {
                        var tpChart = new TabPage();
                        tpChart.Text = i.Key;
                        //WebChartControl chartControl = new WebChartControl();
                        tpChart.Controls.Add(new LiteralControl("<iframe class='iFrameChart' style='width:100%; height:380px;' frameborder='no' framespacing='0' border='0' url='Chart.aspx" + HttpContext.Current.Request.Url.Query + "&_Chart=" + index++ + "' src=''></iframe>"));
                        //tpChart.Controls.Add(chartControl);

                        //ASPxPager paperChart = new ASPxPager();
                        //paperChart.Attributes.Add("Text", i.Key);
                        //paperChart.PageIndexChanging += new PagerPageEventHandler(paperChart_PageIndexChanging);

                        //tpChart.Controls.Add(paperChart);

                        pageControl.TabPages.Add(tpChart);
                    }

                }
                //for (int i = 0; i < chart.PageControlChart.TabPages.Count; i++)
                //{
                //    pageControl.TabPages.Add(chart.PageControlChart.TabPages[i]);
                //}
                //chart.PageControlChart.ContentStyle.Paddings.Padding = 5;
                //}
                //else
                //{
                //    form1.Controls.Add(detailGrid);
                //}

                //detailGrid.SettingsDetail.AllowOnlyOneMasterRowExpanded = true;
            }
            pageControl.ClientSideEvents.TabClick = "function(s,e){if (e.tab.index!=0){" +
                        "var iframe=$(s.mainElement).find(\"tbody > tr:eq(1) > td > div:eq(\"+e.tab.index+\") > iframe\");" +
                        "if (iframe.attr(\"url\")){iframe.attr(\"src\",iframe.attr(\"url\"));iframe.removeAttr(\"url\");}" +
                        "}}";

            pageControl.ClientSideEvents.ActiveTabChanging = "function(s, e) { processOnServer = false;cancel = false;reloadContentOnCallback = false;}";
            pageControl.AutoPostBack = false;
            //uiChart.DataBindToChart(dtSource, StdQuery.QueryName);
            ////if (uiChart.PageControlChart.TabPages.Count != 0)
            ////{


            //uiChart.PageControlChart.TabPages.Insert(0, tpGrid);
            pageControl.Width = new Unit("100%");
            pageControl.Height = new Unit("100%");
            ////UpdatePanel up = new UpdatePanel();
            ////var ct = new ContentTemplate();
            ////ct.control = uiChart.PageControlChart;
            ////up.ContentTemplate = ct;
            //pageControl.ContentStyle.BorderLeft.BorderStyle = BorderStyle.None;
            //pageControl.ContentStyle.BorderRight.BorderStyle = BorderStyle.None;
            //pageControl.ContentStyle.BorderBottom.BorderStyle = BorderStyle.None;
            //pageControl.ContentStyle.Paddings.Padding = 0;
            pageControl.ContentStyle.Paddings.PaddingTop = 5;

            form1.Controls.Add(pageControl);
        }
    }
}